package manage.service;

import m.common.model.util.ModelUpdateUtil;
import m.common.model.util.ModelUtil;
import m.common.service.Service;
import m.system.cache.CacheUtil;
import m.system.util.ClassUtil;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import manage.model.AdminSetting;
import manage.model.SystemInfo;

public class AdminSettingService extends Service {

    public AdminSetting getUniqueModel(String adminOid) throws Exception{
        if(StringUtil.isSpace(adminOid)) return new AdminSetting();
        AdminSetting setting = CacheUtil.get(AdminSetting.class,adminOid);
        if(null==setting) {
            setting=new AdminSetting();
            setting.setOid(adminOid);
            setting.setTheme("");
            setting.setRadius(3);
            setting.setColor("#1677ff");
            ModelUpdateUtil.insertModel(setting);
        }
        return CacheUtil.get(AdminSetting.class,adminOid);
    }

    /**
     * 获取用户所有设置， 包含系统通用设置
     */
    public JSONMessage getAllSetting(String adminOid) throws Exception {
        JSONMessage setting= ModelUtil.toJSONMessage("",getUniqueModel(adminOid));
        SystemInfo sy=CacheUtil.get(SystemInfo.class,"");
        setting.push("assistLogin",sy.getAssistLogin());
        if(StringUtil.notSpace(sy.getBorderRadius())) setting.push("radius",Integer.parseInt(sy.getBorderRadius()));
        if(StringUtil.notSpace(sy.getPrimaryColor())) setting.push("color",sy.getPrimaryColor());
        if(StringUtil.notSpace(sy.getSuccessColor())) setting.push("successColor",sy.getSuccessColor());
        if(StringUtil.notSpace(sy.getWarningColor())) setting.push("warningColor",sy.getWarningColor());
        if(StringUtil.notSpace(sy.getErrorColor())) setting.push("errorColor",sy.getErrorColor());
        if(StringUtil.notSpace(sy.getInfoColor())) setting.push("infoColor",sy.getInfoColor());
        return setting;
    }
    public void setAttr(String adminOid,String name,String value) throws Exception {
        AdminSetting setting=getUniqueModel(adminOid);
        ClassUtil.setFieldValue(setting,name,value);
        ModelUpdateUtil.updateModel(setting,name);
        CacheUtil.clear(AdminSetting.class,adminOid);
    }
}
